﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Imports System.ComponentModel
Partial Public Class frmAdminConsultants
    Private intSaveNeworEdit As Integer


    Private Sub frmAdminConsultants_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        refreshList()

        With Me.drpDept
            .SelectedValuePath = "Department_ID"
            .DisplayMemberPath = "Department_Name"
            .ItemsSource = CType(Admin.DepartmentList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        With Me.drpDisclaimer
            .SelectedValuePath = "disclaimer_id"
            .DisplayMemberPath = "disclaimer_title"
            .ItemsSource = CType(Admin.DisclaimerList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        With Me.drpOISC
            .SelectedValuePath = "oisc_id"
            .DisplayMemberPath = "oisc_description"
            .ItemsSource = CType(Admin.OISCList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        With Me.drpSuper
            .SelectedValuePath = "Con Id"
            .DisplayMemberPath = "Name"
            Dim consultantList As DataTable = Admin.ConsultantList(True)
            Dim firstRow As DataRow
            firstRow = consultantList.NewRow()
            firstRow.BeginEdit()

            firstRow.EndEdit()
            .ItemsSource = CType(consultantList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        With Me.drpBranch
            .SelectedValuePath = "Branch_ID"
            .DisplayMemberPath = "Branch_Name"
            .ItemsSource = CType(Admin.BranchList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        With Me.drpSecurityLevel
            .SelectedValuePath = "Security_ID"
            .DisplayMemberPath = "Security_Name"
            .ItemsSource = CType(Admin.SecurityList, IListSource).GetList()
            .SelectedIndex = -1
        End With

        grpDetails.IsEnabled = False
        btnCancel.IsEnabled = False
        btnClose.IsEnabled = True
        btnEdit.IsEnabled = False
        btnSave.IsEnabled = False
        AddHandler grdConsult.SelectionChanged, AddressOf grdConsult_SelectedRowsChanged

    End Sub

    Private Sub grdConsult_SelectedRowsChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim myCon As CommonDataFunctions.ConsultantInfo
        If Me.grdConsult.SelectedItems.Count = 0 Then
            Me.grpDetails.IsEnabled = False
            Me.btnEdit.IsEnabled = False
            Me.btnCancel.IsEnabled = False
            Exit Sub
        Else
            'Me.grpDetails.IsEnabled = False
            Me.btnEdit.IsEnabled = True
            Me.btnCancel.IsEnabled = True
        End If
        Dim selectedRow As DataRow = grdConsult.SelectedItem
        myCon = Admin.GetConsultantInfo(selectedRow(0))

        If Not IsNothing(myCon) Then
            With myCon
                Me.txtConName.Text = .ConsultantName
                Me.txtJob.Text = .JobTitle
                Me.txtDDI.Text = .DDI
                Me.txtTeamDDI.Text = .DDIUK
                Me.txtEmail.Text = .Email
                Me.drpDept.SelectedValue = .DepartmentId
                Me.drpSuper.SelectedValue = .SupervisorId
                Me.drpOISC.SelectedValue = .OISCId
                Me.txtAccredit.Text = .Accreditation
                Me.drpDisclaimer.SelectedValue = .DisclaimerId
                If .StatusId = 0 Then
                    Me.drpStatus.Text = "Inactive"
                ElseIf .StatusId = 1 Then
                    Me.drpStatus.Text = "Active"
                End If
                Me.txtNTUser.Text = .NTUser
                Me.drpSecurityLevel.SelectedValue = .AccessLevel
                Me.drpBranch.SelectedValue = .BranchID
                Me.txtPassword.Password = .Password
            End With

        End If

    End Sub

    Private Sub refreshList()
        Try
            Me.grdConsult.ItemsSource = Admin.ConsultantList
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminConsultants-refreshList")
            Exit Sub
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnEdit.Click
        intSaveNeworEdit = 2
        Me.grpDetails.IsEnabled = True
        Me.grdConsult.IsEnabled = False
        Me.btnSave.IsEnabled = True
        Me.btnClose.Visibility = Windows.Visibility.Visible
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        Me.grpDetails.IsEnabled = False
        Me.grdConsult.IsEnabled = True
        Me.btnSave.IsEnabled = False
        Me.btnAddNew.IsEnabled = True
        Me.btnClose.Visibility = Windows.Visibility.Visible
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        intSaveNeworEdit = 1
        Me.grdConsult.IsEnabled = False
        Me.ClearControls()
        Me.grpDetails.IsEnabled = True
        Me.btnSave.IsEnabled = True
        Me.btnAddNew.IsEnabled = False
        Me.drpStatus.SelectedIndex = 0
        drpSuper.SelectedValue = 11
        drpOISC.SelectedValue = 4
        drpDisclaimer.SelectedValue = 1
        txtEmail.Text = "."
        Me.btnCancel.IsEnabled = True
        Me.btnClose.Visibility = Windows.Visibility.Visible
        'Me.drpSuper.SelectedValue = "Unassigned"
    End Sub

    Private Sub ClearControls()
        Me.txtConName.Text = String.Empty
        Me.txtJob.Text = String.Empty
        Me.txtDDI.Text = String.Empty
        Me.txtTeamDDI.Text = String.Empty
        Me.txtEmail.Text = String.Empty
        Me.drpDept.SelectedIndex = -1
        Me.drpSuper.SelectedIndex = -1
        Me.drpOISC.SelectedIndex = -1
        Me.txtAccredit.Text = String.Empty
        Me.drpDisclaimer.SelectedIndex = -1
        Me.drpStatus.SelectedIndex = -1
        Me.txtNTUser.Text = String.Empty
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        If txtConName.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Please enter consultant name !", CommonDialog.CustomDialogIcons.Information, "")
            txtConName.Focus()
            Exit Sub
        ElseIf txtPassword.Password.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Password cannot be empty!", CommonDialog.CustomDialogIcons.Information, "")
            txtPassword.Focus()
            Exit Sub
        ElseIf drpDept.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Please select department !", CommonDialog.CustomDialogIcons.Information, "")
            Exit Sub
        ElseIf drpBranch.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Please select consultant branch !", CommonDialog.CustomDialogIcons.Information, "")
            Exit Sub
        ElseIf drpSecurityLevel.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Please select security level !", CommonDialog.CustomDialogIcons.Information, "")
            Exit Sub
        Else
            btnSave.IsEnabled = False
            Dim isSuperUserForFinance As Integer
            If (drpSecurityLevel.SelectedValue = SecurityLevel.AdminUser) Then
                isSuperUserForFinance = 1
            End If
            'grdConsult.Enabled = True
            Dim sql As StringBuilder
            Dim intStatus As Integer

            If Me.drpStatus.Text = "Active" Then
                intStatus = 1
            Else
                intStatus = 0
            End If

            Select Case intSaveNeworEdit
                Case 1 'new one
                    sql = New StringBuilder


                    With sql
                        .Append("")
                        .Append("Insert Into L_Consultant ")
                        .Append("( con_name, Con_Jobtitle,")
                        .Append(" Con_DDI, con_DDIUK, Con_EMail,")
                        .Append(" Con_Department, Con_SupervisorID,")
                        .Append(" Con_OISC, Con_Accreditation, Con_DisclaimerID,")
                        .Append(" Con_Status, Con_NTUsername, con_branchid,Con_SecurityLevel,con_CreditCharges,con_IssueRefund,con_Password)")

                        .Append(" Values(")

                        .Append(" '" & Me.txtConName.Text & "',")
                        .Append(" '" & txtJob.Text & "',")
                        .Append(" '" & txtDDI.Text & "',")
                        .Append(" '" & txtTeamDDI.Text & "',")
                        .Append(" '" & txtEmail.Text & "',")
                        .Append(Me.drpDept.SelectedValue & ", ")
                        .Append(Me.drpSuper.SelectedValue & ", ")
                        .Append(Me.drpOISC.SelectedValue & ", ")
                        .Append(" '" & txtAccredit.Text & "',")
                        .Append(Me.drpDisclaimer.SelectedValue & ", ")
                        .Append(intStatus & ", ")
                        .Append(" '" & txtNTUser.Text & "',")
                        .Append(Me.drpBranch.SelectedValue & ", ")
                        .Append(Me.drpSecurityLevel.SelectedValue & ", ")
                        .Append(isSuperUserForFinance & ", ")
                        .Append(isSuperUserForFinance & ", '")
                        .Append(txtPassword.Password & "')")
                    End With
                    Try
                        If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                            refreshList()
                            CommonFuncs.DisplayMessage("The new user has been added successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                            'Exit Sub
                        Else
                            CommonFuncs.DisplayMessage("There is an error adding the user to the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                            'Exit Sub
                        End If
                    Catch ex As Exception
                        CommonFuncs.ErrorHandler(ex, "frmAdminConsultants-btnSave_Click")
                        Exit Sub
                    End Try

                Case 2 'existing one
                    sql = New StringBuilder
                    With sql
                        .Append("update l_consultant set ")
                        .Append(" con_name = '" & Me.txtConName.Text & "',")
                        .Append(" Con_Jobtitle = '" & txtJob.Text & "',")
                        .Append(" Con_DDI = '" & txtDDI.Text & "',")
                        .Append(" con_DDIUK = '" & txtTeamDDI.Text & "',")
                        .Append(" Con_EMail = '" & txtEmail.Text & "',")
                        .Append(" Con_Department = " & Me.drpDept.SelectedValue & ", ")
                        If (Me.drpSuper.SelectedIndex = -1) Then
                        Else
                            .Append(" Con_SupervisorID = " & Me.drpSuper.SelectedValue & ", ")
                        End If
                        .Append(" Con_OISC = " & Me.drpOISC.SelectedValue & ", ")
                        .Append(" Con_Accreditation = '" & txtAccredit.Text & "',")
                        .Append(" Con_DisclaimerID = " & Me.drpDisclaimer.SelectedValue & ", ")
                        .Append(" Con_Status = " & intStatus & ", ")
                        .Append(" Con_NTUsername = '" & txtNTUser.Text & "',")
                        .Append(" con_branchid = " & Me.drpBranch.SelectedValue & ", ")
                        .Append(" Con_SecurityLevel = " & Me.drpSecurityLevel.SelectedValue & ", ")
                        .Append("con_CreditCharges =" & isSuperUserForFinance & ", ")
                        .Append("con_Password='" & txtPassword.Password & "',")
                        .Append("con_IssueRefund=" & isSuperUserForFinance)
                        Dim selectedRow As DataRow = grdConsult.SelectedItem
                        .Append(" where con_id = " & selectedRow(0))
                    End With

                    Try
                        If CommonDataFunctions.ExecuteSQL(sql.ToString) = True Then
                            refreshList()
                            CommonFuncs.DisplayMessage("The user's details have been updated successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                            'Exit Sub
                        Else
                            CommonFuncs.DisplayMessage("There is an error in editing the user's details. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                            'Exit Sub
                        End If
                    Catch ex As Exception
                        CommonFuncs.ErrorHandler(ex, "frmAdminConsultants-btnSave_Click")
                        Exit Sub
                    End Try


            End Select

            grpDetails.IsEnabled = False
            grdConsult.IsEnabled = True
            btnCancel.IsEnabled = False
        End If

    End Sub
End Class
